package com.gm.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by taojie on 17-6-7.
 */
public class DateUtils {

    /**
     * 获取当前的时间戳
     * @return
     */
    public static long getCurrentUnixTime(){
        return new Date().getTime();
    }

    public static long formatDatedd(long unixTime){
        SimpleDateFormat fmt=new SimpleDateFormat("dd");
        return Long.parseLong(fmt.format(unixTime));
    }

    public static long formatDateyyyyMM(long unixTime){
        SimpleDateFormat fmt=new SimpleDateFormat("yyyyMM");
        return Long.parseLong(fmt.format(unixTime));
    }

    public static long formatDateHH(long unixTime){
        SimpleDateFormat fmt=new SimpleDateFormat("HH");
        return Long.parseLong(fmt.format(unixTime));
    }

    public static long formatDateyyyyMMdd(long unixTime){
        SimpleDateFormat fmt=new SimpleDateFormat("yyyyMMdd");
        return Long.parseLong(fmt.format(unixTime));
    }


    public static long formatDateyyyyMMddHH(long unixTime){
        SimpleDateFormat fmt=new SimpleDateFormat("yyyyMMddHH");
        return Long.parseLong(fmt.format(unixTime));
    }

    public static long formatDateyyyyMMddHHmmss(long unixTime){
        SimpleDateFormat fmt=new SimpleDateFormat("yyyyMMddHHmmss");
        return Long.parseLong(fmt.format(unixTime));
    }

    public static long getMonthFirstDayUnixTime(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0 + num);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTimeInMillis();
    }

    /**
     * 以当月为纬度,获取相差num月的最后天最后一刻的时间戳
     * @param num
     * @return
     */
    public static long getMonthLastDayUnixTime(int num){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1 + num);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取两个月的月数差
     * @param date1  时间戳
     * @param date2  时间戳
     * @return
         * @throws ParseException
     */
    public static int getMonthSpace(long date1, long date2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(sdf.parse(sdf.format(date1)));
        c2.setTime(sdf.parse(sdf.format(date2)));
        if(c1.getTimeInMillis() < c2.getTimeInMillis()) return 0;
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        // int day1 = c1.get(Calendar.DAY_OF_MONTH);
        // int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if(month1 < month2 || month1 == month2) yearInterval --;
        // 获取月数差值
        int monthInterval =  (month1 + 12) - month2  ;
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }

//    public static long datepicker2Unix(String s){
//        long unixTime = 0;
//        try {
//            String[] arr=s.split("/");
//            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String time=arr[2] + "-" + arr[0]+ "-" + arr[1] + " 00:00:00";
//            Date date = format.parse(time);
//            unixTime = date == null? 0:date.getTime();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return unixTime;
//    }

}
